package com.situ.day10;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ExceptionDemo {

    //ArithmeticException 算数异常
    @Test
    public void test1() {
        //运行时异常 RuntimeException
        //java.lang.ArithmeticException: / by zero
        //int num = 3 / 0;

        String str = null;
        //NullPointerException
        str.length();
    }

    @Test
    public void test4() {
        //编译时异常: 写代码编译时候就报错，未雨绸缪
        //Unhandled exception: java.io.FileNotFoundException
        try {
            //try:可能会抛出异常的代码
            FileInputStream fileInputStream = new FileInputStream("a1.txt");
        } catch (FileNotFoundException e) {
            //catch:如果抛出异常就走到catch里面
            throw new RuntimeException(e);
        } finally {
            //finally:不管抛没抛出异常，一定会执行的代码
            System.out.println("finally");
        }
    }

    //编译时异常都是集成自Exception
    //运行时异常：都继承自RuntimeException
    // NullPointerException extends RuntimeException
    @Test
    public void test66() {
        try {
            fun();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private void fun() throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("a.txt");
    }
}
